package com.intexh.speedandroid.utils;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.intexh.speedandroid.R;

/**
 * Created by FrankZhang on 2017/11/16.
 */

public class DialogUtils {

    public static void showDefaultDialog(Context context, String title, String content, final DialogImpl listener) {
        showDefaultDialog(context, title, content, "确定", "取消", listener);
    }

    public static void showDefaultDialog(Context context, String title, String content, String ok, String cancel, final DialogImpl listener) {
        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(content);
        builder.setNegativeButton(cancel, (dialog, which) -> listener.onCancel());
        builder.setPositiveButton(ok, (dialog, which) -> listener.onOk());
        builder.show();
    }

    public static Dialog showHitDialog(Context context,String title, String content){
        return showHitDialog(context,title, content,null);
    }
    public static Dialog showHitDialog(Context context,String title, String content,DialogImpl impl){
        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(content);
//        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", (dialog, which) -> {
            if(impl!=null){
                impl.onOk();
            }
        });
        return builder.show();
    }
    public static class DialogImpl{
        public void onOk(){}
        public void onCancel(){}
    }


    /**
     * 底部菜单选择
     *
     * @param context
     * @param menu1_str
     * @param menu2_str
     * @param impl
     */
    public static void showBottomMenuDialog(Context context, String title, String menu1_str, String menu2_str, BottomMenuDialogImpl impl) {
        final Dialog mBottomSheetDialog = new Dialog(context, R.style.MaterialDialogSheet);
        View view = View.inflate(context, R.layout.dialog_bottom_tow_menu_sheet, null);
        TextView title_tv =  view.findViewById(R.id.title_tv);
        title_tv.setText(title);
        TextView menu1 =  view.findViewById(R.id.menu1);
        TextView menu2 =  view.findViewById(R.id.menu2);
        menu1.setText(menu1_str);
        menu2.setText(menu2_str);
        TextView cancel =  view.findViewById(R.id.cancel);

        menu1.setOnClickListener(v -> {
            mBottomSheetDialog.dismiss();
            impl.onMenu1();
        });
        menu2.setOnClickListener(v -> {
            mBottomSheetDialog.dismiss();
            impl.onMenu2();
        });

        cancel.setOnClickListener(v -> mBottomSheetDialog.dismiss());

        mBottomSheetDialog.setContentView(view);
        mBottomSheetDialog.setCancelable(true);
        mBottomSheetDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);
        mBottomSheetDialog.show();
    }
    public interface BottomMenuDialogImpl {
        void onMenu1();
        void onMenu2();
    }

    public static void showEditTextDialog(Context context,String title,String content,onEditImpl impl){
        View view = View.inflate(context,R.layout.edittext_dialog_layout,null);
        TextView title_tv = view.findViewById(R.id.title_tv);
        ImageView close_iv = view.findViewById(R.id.close_iv);
        EditText content_et = view.findViewById(R.id.content_et);
        TextView ok_tv = view.findViewById(R.id.ok_tv);
        title_tv.setText(title);
        if(!TextUtils.isEmpty(content)){
            content_et.setText(content);
            content_et.setSelection(content.length());
        }
        Dialog dialog = showFullScreenViewDialog(context, view, true, true);
        ok_tv.setOnClickListener(v -> {
            if(TextUtils.isEmpty(content_et.getText().toString())){
                ToastUtil.showToast(context,"请输入内容");
                return;
            }
            impl.onEdit(content_et.getText().toString());
            dialog.dismiss();
        });
        view.setOnClickListener(v -> {
            dialog.dismiss();
        });
        close_iv.setOnClickListener(v -> {
            dialog.dismiss();
        });
    }
    public interface onEditImpl{
        void onEdit(String str);
    }


    /**
     * 显示全屏的dialog dialog本身是透明，颜色再view中定义
     *
     * @param context
     * @param content
     * @param isCancelable             是否可以点击返回键取消
     * @param isCanceledOnTouchOutside 点击dialog意外其他区域是否可以取消
     * @return
     */
    public static Dialog showFullScreenViewDialog(Context context, View content,
                                                  boolean isCancelable, boolean isCanceledOnTouchOutside) {
        Dialog dialog = new Dialog(context, R.style.transparent_dialog_theme);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(isCancelable);         //是否可以取消
        dialog.setCanceledOnTouchOutside(isCanceledOnTouchOutside); //点击弹窗外是否消失
        dialog.setContentView(content);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = UIUtil.getWindowWidth(context); // 宽度
        lp.height = UIUtil.getWindowHeight(context); // 高度
        dialogWindow.setAttributes(lp);
        dialog.show();
        return dialog;
    }
}
